//
//  SuggestViewController.m
//  Product_NEWS
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 FCH. All rights reserved.
//

#import "SuggestViewController.h"
#import "SuggestView.h"
#import "UserViewController.h"
@interface SuggestViewController ()<UITextFieldDelegate, UITextViewDelegate>

@property (nonatomic, retain) SuggestView *rootView;
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain)     UILabel *label;
@end

@implementation SuggestViewController

    - (void)dealloc {
        [_label release];
        [_textField release];
        [_rootView release];
        [super dealloc];
    }


-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];
   
    
}


- (void)loadView {
    self.rootView = [[[SuggestView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.view = _rootView;
    
   self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.949 alpha:1.000];
    
    
    
}

- (void)viewDidLoad {
       [super viewDidLoad];
        self.label = [[[UILabel alloc]initWithFrame:CGRectMake(3 * SCREEN_WIDTH, 4 *SCREEN_HEIGHT, 300* SCREEN_WIDTH, 20*SCREEN_HEIGHT)] autorelease];
        self.label.enabled = NO;
        self.label.text = @"请反馈问题或建议，帮助我们不断改进！";
        self.label.font =  [UIFont systemFontOfSize:15*SCREEN_WIDTH];
        self.label.textColor = [UIColor colorWithRed:225 / 255.0 green:220 / 255.0 blue:225 / 255.0 alpha:1.0];
        [self.rootView.content addSubview:self.label];
        
        
        self.view.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1.0];
        self.navigationItem.title = @"意见反馈";
        
        [self configureRightButton];
        _rootView.content.delegate = self;
        _rootView.email.delegate = self;
    
    
  
    }
    
  
    
    
#pragma mark UITextFileDelegate
    
    //点击空白区域，会触发的方法
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        //回收键盘
        [self.textField resignFirstResponder];
    }
    
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        self.textField = textField;
        return YES;
    }
    
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
        return YES;
    }
    
#pragma mark - UITextViewDelegate设置默认文字
    
    - (void)textViewDidChange:(UITextView *)textView {
        
        if (textView.text.length == 0) {
            [self.label setHidden:NO];
        } else {
            [self.label setHidden:YES];
        }
    }
#pragma mark - 界面配置类

  - (void)configureRightButton {
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(handleSend:)];
        self.navigationItem.rightBarButtonItem = right;
        [right release];
        
    }

    
- (void)handleSend:(UIBarButtonItem *)item {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"反馈提醒" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        if (_rootView.content.text.length  < 5) {
            alert.message = @"别吝啬，多留点字吧~";
        } else {
            alert.message = @"请输入联系方式";
          
        }
        [alert show];
        [alert release];
    }

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    
    if ([MFMailComposeViewController canSendMail]) { // 用户已设置邮件账户
        [self sendEmailAction]; // 调用发送邮件的代码
    }
    
    
}









#pragma mark - 在应用内发送邮件 


- (void)sendEmailAction
{
    // 邮件服务器
    MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
    // 设置邮件代理
    [mailCompose setMailComposeDelegate:self];
    
    // 设置邮件主题
    [mailCompose setSubject:@"主题"];
    
    // 设置收件人
    [mailCompose setToRecipients:@[@"396256111@qq.com"]];
    // 设置抄送人
    [mailCompose setCcRecipients:@[@"邮箱"]];
    // 设置密抄送
    [mailCompose setBccRecipients:@[@"396256111@qq.com"]];
    
    /**
     *  设置邮件的正文内容
     */
    NSString *emailContent = @"我是邮件内容";
    // 是否为HTML格式
    [mailCompose setMessageBody:emailContent isHTML:NO];
    // 如使用HTML格式，则为以下代码
    //    [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World！</p></body></html>" isHTML:YES];
    
 

    
    // 弹出邮件发送视图
    [self presentViewController:mailCompose animated:YES completion:nil];
}



- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{
    switch (result)
    {
        
        case MFMailComposeResultSent: // 用户点击发送
            NSLog(@"Mail sent...");
            break;
        case MFMailComposeResultFailed: // 用户尝试保存或发送邮件失败
            NSLog(@"Mail send errored: %@...", [error localizedDescription]);
            break;
        case MFMailComposeResultCancelled: // 用户取消编辑
            NSLog(@"Mail send canceled...");
            break;
        case MFMailComposeResultSaved: // 用户保存邮件
            NSLog(@"Mail saved...");
            break;
         
    }
    
////    // 关闭邮件发送视图
//       [self dismissViewControllerAnimated:YES completion:nil];
//    
}















/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
